package com.leetcode;

public class DecodeWays
{

	public static void main(String[] args)
	{
		DecodeWays dw = new DecodeWays();
		// dw.numDecodings("2789");
		System.out.println(dw.numDecodings1("27"));
	}

	public int numDecodings(String s)
	{
		// Start typing your Java solution below
		// DO NOT write main() function
		if (s == null || s.length() == 0)
			return 0;
		if (s.length() == 1)
		{
			if (s.equals("0"))
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}

		if (s.length() == 2)
		{
			if (s.charAt(0) == '0')
				return 0;

			int value = Integer.parseInt(s);
			if (value == 10 || value == 20)
				return 1;
			if (value <= 26)
				return 2;
			if (value % 10 == 0)
				return 0;
			return 1;
		}

		int preV = numDecodings(s.substring(0, 2)) > 0 ? 1 : 0;
		System.out.println(preV + "");
		return numDecodings(s.substring(0, 1))
				* numDecodings(s.substring(1, s.length())) + preV
				* numDecodings(s.substring(2, s.length()));
	}

	public int numDecodings1(String s)
	{
		if (s.length() == 0)
			return 0;
		int len = s.length();
		if (len < 1)
			return 0;
		int[] numarray = new int[len];
		int i = len - 1;
		numarray[i] = (s.charAt(i) == '0') ? 0 : 1;
		i--;
		while (i >= 0)
		{
			
			if (s.charAt(i) == '0')
			{
				numarray[i] = 0;
			}
				
			else if (s.charAt(i) == '1'|| (s.charAt(i) == '2' && s.charAt(i + 1) <= '6'))
			{
				OutTxt.Write(s.charAt(i)+"");
				if (i == len - 2)
				{
					numarray[i] = numarray[i + 1] + 1;
				}
				else
				{
					numarray[i] = numarray[i + 1] + numarray[i + 2];
				}
			}
			else
			{
				OutTxt.Write(s.charAt(i)+"");
				numarray[i] = numarray[i + 1];
				
			}
			i--;
		}
		for (int j : numarray)
		{
			OutTxt.Write(j+"...");
		}
		
		return numarray[0];
	}
}
